package cn.aitstone.org.service.impl;

import cn.aitstone.basic.service.impl.BaseServiceImpl;
import cn.aitstone.basic.utils.PageList;
import cn.aitstone.org.domain.Department;
import cn.aitstone.org.mapper.DepartmentMapper;
import cn.aitstone.org.query.DepartmentQuery;
import cn.aitstone.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/**
 * @Description: service层实现类
 * @Author: Sheldon
 * @Version: 1.0
 * @Date:  2022/6/21 19:33
 */
@Service
// 只读事务
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;



    /**
    * @Title: save
    * @Description: 保存方法
    * @Author: Sheldon
    * @Version: 1.0
    * @Date:  2022/6/21 19:35
    * @Parameters: [department]
    * @Return void
    */
    @Override
    @Transactional
    public void save(Department department) {
        departmentMapper.save(department);
        System.out.println("验证"+department);
        // 给dirPath拼接值
        String dirPath = "";
        if (department.getParent().getId() == null){
            // 如果parent对象为Null,说明他是顶级对象，那么dirPath就是他自己的主键id
            dirPath = "/" + department.getId();
        }else {
            // 否则他就有上级部门，那么我们需要查询到上级部门的对象，获取到上级部门的dirpath，拼接我自己的id，就是我的dirpath
            Department parentDept = departmentMapper.loadById(department.getParent().getId());
            if(parentDept != null){
                dirPath = parentDept.getDirPath() + "/" + department.getId();
            }
        }
        department.setDirPath(dirPath);
        System.out.println("验证后" + department);
        departmentMapper.update(department);
    }

    /**
    * @Title: update
    * @Description: 修改
    * @Author: Sheldon
    * @Version: 1.0
    * @Date:  2022/6/21 19:36
    * @Parameters: [department]
    * @Return void
    */
    @Override
    @Transactional
    public void update(Department department) {
        // 给dirPath拼接值
        String dirPath = "";
        if (department.getParent().getId() == null){
            // 如果parent对象为Null,说明他是顶级对象，那么dirPath就是他自己的主键id
            dirPath = "/" + department.getId();
        }else {
            // 否则他就有上级部门，那么我们需要查询到上级部门的对象，获取到上级部门的dirpath，拼接我自己的id，就是我的dirpath
            Department parentDept = departmentMapper.loadById(department.getParent().getId());
            if(parentDept != null){
                dirPath = parentDept.getDirPath() + "/" + department.getId();
            }
        }
        department.setDirPath(dirPath);
        departmentMapper.update(department);
    }




    /**
    * @Title: tree
    * @Description: 获取部门树数据
    * @Author: Sheldon
    * @Version: 1.0
    * @Date:  2022/6/25 20:49
    * @Parameters: []
    * @Return java.util.List<cn.aitstone.org.domain.Department>
    */
    @Override
    public List<Department> tree() {
        List<Department> deptAll = departmentMapper.loadAll();
        ArrayList<Department> deptTree = new ArrayList<>();
        HashMap<Long, Department> deptMap = new HashMap<>();
        for (Department department : deptAll) {
            deptMap.put(department.getId(), department);

        }
        for (Department deptTemp : deptAll){
            if(deptTemp.getParentId() == null){
                deptTree.add(deptTemp);
            } else {
                // 从map集合中获取到父对象，将自己添加到父对象的children集合中
                Department department = deptMap.get(deptTemp.getParentId());
                if (department != null){
                    department.getChildren().add(deptTemp);
                }
            }

        }
        return deptTree;

    }
}
